﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BudgetManager.Domain;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
using Coding4Fun.Phone.Controls;

namespace BudgetManager
{
    public partial class Main : PhoneApplicationPage
    {
        private object ok; //ApplicationBar  ok button
        private object cancel; //ApplicationBar  cancel button
        ObservableCollection<Account> dispAccounts; // observable .. para a listbox ser actulizada dinamicamente


        public Main()
        {
            InitializeComponent();
            dispAccounts = new ObservableCollection<Account>(AccountManager.ReadAllAccounts());
            listBoxUsers.ItemsSource = dispAccounts;
            ok = ApplicationBar.Buttons[0];
            cancel = ApplicationBar.Buttons[1];
            ApplicationBar.Buttons.Clear();
        }

        //verifica se o novo registo tem no minimo o nome
        private void checkF(object sender, RoutedEventArgs e)
        {
            if (txtAccountName.Text == "")
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;//ok a false
            }
            else
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;//ok a false    
        }

        private void password()
        {

            if (((Account)listBoxUsers.SelectedItem).Pin == "") {
                AccountManager.SelectedAccount = (Account)listBoxUsers.SelectedItem;
                NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
                return;
            }
            PasswordInputPrompt passwordInput = new PasswordInputPrompt
            {
                Title = "Insert Your Pin,Please",
                IsCancelVisible = true,
            };         
            passwordInput.Completed += input_Completed;
            passwordInput.Show();
            
        }

       

        void input_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Cancelled)return;

            if (((PasswordInputPrompt)sender).Value == ((Account)listBoxUsers.SelectedItem).Pin)
            {
                AccountManager.SelectedAccount = (Account)listBoxUsers.SelectedItem;
                NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
            }
            else
            {
                MessagePrompt wrongPass = new MessagePrompt
                {
                    Title = "Wrong Pin !!!",
                    Message = "Do you want to try Again?",
                    IsCancelVisible=true,    
                    
                };
                wrongPass.Completed +=  messagePrompt_Completed;
                wrongPass.Show();
            }
        }

        void messagePrompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Cancelled) return;
            else password();
        }
        //clik no ok
        private void onOk(object sender, EventArgs e)
        {

            if (Lolvot.SelectedIndex == 0)
                password();
            else
            {
                if (!checkPinAndRepeatPin())
                {
                   MessagePrompt wrongPin = new MessagePrompt
                    {
                        Title = "Mismatch Pin and Repeat Pin",
                        Message = "Pin and repeat pin not corresponding. Insert again",
                        IsCancelVisible = false,
                        
                    };
                   wrongPin.Show();
                   return;
                }
                this.Focus();
                Lolvot.SelectedIndex = 0;
                AccountManager.CreateAccount(txtAccountName.Text, txtPin.Password, txtDescription.Text);
                dispAccounts.Add(AccountManager.getLast());

            }

        }

        //clik no ok
        private void onCancel(object sender, EventArgs e)
        {
            this.Focus();
            Lolvot.SelectedIndex = 0;
        }

        private void onchngeonPivot(object sender, PivotItemEventArgs e)
        {
            this.Focus();//esconder o teclado
            if (Lolvot.SelectedIndex == 1)
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(ok);//ok a 
                ApplicationBar.Buttons.Add(cancel);
                checkF(sender, null);
            }
            else
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(ok);
                if (listBoxUsers.SelectedItem != null)//verifica se alguma conta esta selecionada
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
                else
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;
            }
        }
        //mesma funçao que checkF mas evento diferente
        private void checkFW(object sender, TextChangedEventArgs e)
        {
            checkF(sender, null);
        }
        // selecçao com double tap
        private void sellectAndGo(object sender, System.Windows.Input.GestureEventArgs e)
        {
            AccountManager.SelectedAccount = (Account)listBoxUsers.SelectedItem;
            NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
        }

        //tap
        private void onSelection(object sender, SelectionChangedEventArgs e)
        {
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
        }

        private  bool checkPinAndRepeatPin(){
            return txtPin.Password.Equals(txtRepeatPin.Password);
        }
    }
}
